import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Vector;

import javax.swing.*;


public class SettingFrame extends JFrame
{
	//Vectors used for dropdown menu
	private Vector<String> Employee = new Vector<String>();
	private Vector<Employee> EmployeeInfo;
	
	//GUI Component
	private JComboBox reviewerComboBox;
	
	private JLabel nameLabel;
	private JLabel nameLabel1;
	private JLabel passwordLabel;
	
	private JLabel workTimeLabel;
	private JLabel workTimeLabel1;
	
	private JTextField passwordField;
	
	private JButton editPasswordButton;
	private JButton saveButton;
	private JButton cancelButton;
	
	private backgroundPanel panel;
	private JPanel entryPanel;
	private JPanel savePanel;
	
	//Handlers
	private SelectHandler selectHandler;
	private EditHandler editHandler;
	private SaveHandler saveHandler;
	private CancelHandler cancelHandler;
	
	// data control
	private mainFrame backScreen;
	private UserManager um;
	private Employee selected;
	/**
	 * Constructor
	 */
	public SettingFrame(mainFrame initBackScreen, UserManager initUm)
	{
		backScreen = initBackScreen;
		um = initUm;
		intiData();
		layoutGUI();
		//setSize(620, 400);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	
	/**
	 * Initial data, ie., reviewer list, research area and country
	 */
	public void intiData()
	{				
		EmployeeInfo = um.getEmployeeList();
		Employee.add("");
		for (int i=0; i<EmployeeInfo.size(); i++)
			Employee.add(EmployeeInfo.get(i).getName());
	}
	
	public void layoutGUI()
	{
		//Construct GUI components
		this.setTitle("Settings");
		this.setResizable(false);
		this.setSize(540, 420);
		panel = new backgroundPanel("background7.jpg",0,0,540, 420);
		
		reviewerComboBox = new JComboBox(Employee);
		reviewerComboBox.setBackground(Color.WHITE);
		
		nameLabel= new JLabel("Name: ");
		nameLabel1 = new JLabel();
		passwordLabel = new JLabel("Password: ");
		
		passwordField = new JTextField(10);
		passwordField.setEditable(false);
		editPasswordButton = new JButton("Edit");
		editPasswordButton.setEnabled(false);
		
		if(!um.getCurrentUser().getmanagerCheck())
		passwordField.hide();
		
		workTimeLabel = new JLabel("WorkTime: ");
		workTimeLabel1 = new JLabel();
		
		saveButton = new JButton("Save");
		saveButton.setEnabled(false);
		
		cancelButton = new JButton("Cancel");
		cancelButton.setEnabled(true);
		
		entryPanel = new JPanel();
		//savePanel = new JPanel();
		entryPanel.setBackground(Color.WHITE);
		//savePanel.setBackground(Color.WHITE);
		
		//savePanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
		//savePanel.add(saveButton);
		
		//Setup layout
		GridBagLayout gbl = new GridBagLayout();
		entryPanel.setLayout(gbl);
		
		addJComponentToContainerUsingGBL(reviewerComboBox, 				entryPanel,	0,	0,	1,	1);
		addJComponentToContainerUsingGBL(nameLabel, 					entryPanel,	0,	1,	1,	1);
		addJComponentToContainerUsingGBL(nameLabel1, 					entryPanel,	1,	1,	1,	1);
		addJComponentToContainerUsingGBL(passwordLabel, 				entryPanel,	0,	2,	1,	1);
		addJComponentToContainerUsingGBL(passwordField, 				entryPanel,	1,	2,	1,	1);
		addJComponentToContainerUsingGBL(editPasswordButton, 			entryPanel,	2,	2,	1,	1);
		addJComponentToContainerUsingGBL(workTimeLabel,					entryPanel,	0,	3,	1,	1);
		addJComponentToContainerUsingGBL(workTimeLabel1,				entryPanel,	1,	3,	1,	1);
		addJComponentToContainerUsingGBL(saveButton,					entryPanel,	1,	4,	1,	1);
		addJComponentToContainerUsingGBL(cancelButton,					entryPanel,	2,	4,	1,	1);
		
		//this.add(savePanel, BorderLayout.SOUTH);
		//this.add(entryPanel, BorderLayout.CENTER);
		panel.add(entryPanel, BorderLayout.CENTER);
		//panel.add(savePanel, BorderLayout.CENTER);
		
		this.setLayout(new BorderLayout());
		this.add(panel, BorderLayout.CENTER);
		
		//Construct handler
		selectHandler = new SelectHandler();
		editHandler = new EditHandler();
		saveHandler = new SaveHandler();
		cancelHandler = new CancelHandler();
		
		//Link component to hander
		reviewerComboBox.addActionListener(selectHandler);
		saveButton.addActionListener(saveHandler);
		cancelButton.addActionListener(cancelHandler);
		editPasswordButton.addActionListener(editHandler);
		
		// size and locate the dialog at the center of the screen
		Toolkit tk = Toolkit.getDefaultToolkit();
		Dimension screenSize = tk.getScreenSize();
		int screenWidth = screenSize.width;
		int screenHeight = screenSize.height;
		int x = (screenWidth - 1080)/2;
		int y = (screenHeight - 620)/2;
		setLocation(x,y);
		
	}
	
	/**
	 * GridBadLayout helper method
	 */
	public void addJComponentToContainerUsingGBL(	JComponent jc,
			Container c,
			int x,
			int y,
			int w,
			int h)
	{
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = x;
		gbc.gridy = y;
		gbc.gridwidth = w;
		gbc.gridheight = h;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.insets = new Insets(5, 5, 5, 5);
		c.add(jc, gbc);
	}
	
	class SelectHandler implements ActionListener{

		public void actionPerformed(ActionEvent arg0) {
			if (((JComboBox) arg0.getSource()).getSelectedItem().equals("")){
				editPasswordButton.setEnabled(false);
				saveButton.setEnabled(false);
				
				nameLabel1.setText("");
				passwordField.setText("");
				workTimeLabel1.setText("");
				
				selected = null;
			}
			else {
				if(um.getCurrentUser().getmanagerCheck())
					editPasswordButton.setEnabled(true);
				saveButton.setEnabled(true);
				
				int index = ((JComboBox) arg0.getSource()).getSelectedIndex()-1;
				selected = EmployeeInfo.get(index);
				
				nameLabel1.setText(selected.getName());
				passwordField.setText(selected.getPassword());
				workTimeLabel1.setText(""+selected.getworktime());
				
			}
			
		}
		
	}
	
	class EditHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent ae)
		{
			//Determine which field need to be changed			
			if(ae.getSource() == editPasswordButton)
			{
				passwordField.setEditable(true);
			}
		}
	}
	
	class SaveHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent ae)
		{
			selected.setPassword(passwordField.getText());
			um.updateUser(selected);
			
			//System.out.println(selected.toString());
			
			SettingFrame.this.setVisible(false);
			mainFrame frame = new mainFrame(um);
			frame.setVisible(true);
		}
	}
	
	class CancelHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent ae)
		{			
			SettingFrame.this.setVisible(false);
			mainFrame frame = new mainFrame(um);
			frame.setVisible(true);
		}
	}
	
}	
